package com.stylefeng.guns.modular.advertisement.service.impl;

import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.advertisement.entity.AppAdDto;
import com.stylefeng.guns.modular.system.model.AppAds;
import com.stylefeng.guns.modular.system.dao.AppAdsMapper;
import com.stylefeng.guns.modular.advertisement.service.IAppAdsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.vpn.config.properties.SftpProperties;
import com.stylefeng.guns.modular.vpn.utils.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-06-11
 */
@Service
public class AppAdsServiceImpl extends ServiceImpl<AppAdsMapper, AppAds> implements IAppAdsService {

    @Resource(name = "imageSftp")
    private SftpProperties imageSftp;

    @Override
    @Transactional
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void createNewAdvertisement(AppAdDto ad) {
        AppAds adv = new AppAds();
        BeanUtils.copyProperties(ad, adv);
        if(adv.getStartTime().compareTo(new Date()) < 0) {
            adv.setStartTime(new Date());
            adv.setStatus(1);
        } else {
            adv.setStatus(0);
        }
        adv.setUpdateTime(new Date());
        String adImgPath = FileUtil.uploadFile(ad.getAdImage(), imageSftp);
        adv.setAdImage(adImgPath);
        baseMapper.insert(adv);
    }
}
